<?php
/*************************************************
 * file description
 * @filename:           StudentInfoModel.class.php
 * @desc:               学生信息表
 * @tables:             [dd_student_info]
 * @date:               2016-9-25
 * @author:             大业
 * @version:            v1.0
 *************************************************/
namespace Common\Model;

use Common\Model\BaseModel;

class StudentInfoModel extends BaseModel
{

	/**
	 * [获取表[student_info]的信息]
	 * @method getInfo
	 * @author 大业
	 * @create 2016-09-25
	 * @param  [string] $condtion [查询条件]
	 * @return [array] [查询信息]
	 */
	public function getInfo($condtion,$pageSet)
	{
        $where = $condtion;
        $field = array(
			'id',				//自增ID
			'sch_id', 			//学校ID
			'stu_no', 			//学生学号
			'stu_name', 		//学生姓名
			'stu_sex',  		//学生性别：1-男,2-女,0-其他
			'stu_birthday', 	//学生生日
			'stu_patriarch_id',	//学生家长-id
			'stu_contact',		//联系方式
			'stu_native_place',	//学生籍贯
			'stu_join_time',  	//入园时间
			'is_schooling', 		//是否在园：0-否，1-是(默认）
			'created', 		//添加时间
        );

        $order = 'id desc';
        return $this->selectData($where,$field,$order,$pageSet);
	}


	/**
	 * [根据传入条件查询学生信息]
	 * @method showInfo
	 * @author 大业
	 * @create 2016-09-25
	 * @param  [int] id [学生id]
	 * @param  [string] $search [搜索信息]
	 * @param  [string] $style [输出样式控制标识:home/admin]
	 * @return [array] [输出信息]
	 */
	public function showInfo($id=null,$search,$style='home')
	{
		//查询条件(传入参数为空时,取所有未删除的数据)
		$where = '1 = 1 ';

		//学生id
		if(!empty($id)){
			$where = $where.' and id='.$id;
		}

		//查询条件
		if(!empty($search)){
			$where = $where.' and '.$search;
		}

		//取表数据
		$pageSet['limit'] = 20;
		$Info = $this->getInfo($where,$pageSet);

		//将显示的信息进行转换并输出
		$List = $this->fmtInfo($Info['list'],$style);
		return array('list'=>$List,'page'=>$Info['page']);
	}


	/**
	 * [格式化输出信息]
	 * @method fmtInfo
	 * @author 大业
	 * @create 2016-08-17
	 * @param  [array] $info [要格式的信息]
	 * @param  [string] $style [常用:common,后台:admin,前台:home(默认)]
	 * @return [array] [返回信息]
	 */
	private function fmtInfo($info,$style)
	{
		if(empty($info)){return null;}
		//常用数据
		if($style == 'common'){
			foreach ($info as $key => $value) {
				$List[$key]['id'] 		= $value['id'];
				$List[$key]['number'] 	= $value['stu_number'];
				$List[$key]['name'] 	= $value['stu_name'];
			}
		//后台取数
		}else if($style == 'admin'){
			$List=$info;
			foreach ($info as $key => $value) {
				//$List[$key]['stu_patriarch']= $this->getPatriarchInfo($value['stu_patriarch_id']);
			}

		//前台取数 (前台出去的数据稍微转换下，以防暴露数据库字段)
		}else{
			foreach ($info as $key => $value) {
				$List[$key]['id'] 			= $value['id'];
				$List[$key]['number'] 		= $value['stu_no'];
				$List[$key]['name'] 		= $value['stu_name'];
				$List[$key]['sex'] 			= $value['stu_sex'];
				$List[$key]['birthday'] 	= $value['stu_birthday'];
				//$List[$key]['patriarch']	= $value['stu_patriarch_id'];
				$List[$key]['contact'] 		= $value['stu_contact'];
				$List[$key]['native_place'] = $value['stu_native_place'];
				$List[$key]['contact'] 		= $value['stu_contact'];
				$List[$key]['join_time'] 	= $value['stu_join_time'];
				$List[$key]['is_working'] 	= $value['is_working'];
				$List[$key]['add_time'] 	= $value['add_time'];
			}
		}
		return $List;
	}

	/**
	 * [统计表数据信息]
	 * @method getCountNum
	 * @author 大业
	 * @create 2016-09-25
	 * @param  [string] $condition [传入条件]
	 * @return [arrayh] [返回统计信息]
	 */
	public function getCountNum($condition)
 	{
 		if(empty($condition)){
 			$condition='1 = 1';
 		}
 		$where = $condition;
 		$cntNum['all'] = $this->countData($where);
 		//总人数包括,在校和离校的，后续再分开处理.
 		// $where = $condition.' and is_del != 1 and is_pass != 0';
 		// $cntNum['use'] = $this->countData($where);
 		// $where = $condition.' and is_del != 1 and is_pass = 0';
 		// $cntNum['unuse']=$this->countData($where);
 		return $cntNum['all'];
 	}


}
